<?php

/**
 * Form for editing parent data
 */
class Application_Form_Parent extends Zend_Form_SubForm
{

    private $emailValidator = null;
    // Mapping keys to db columns
    private static $DB_MAPPING = array(
	"name" => "forename",
	"surname" => "surname",
	"telnumber_prim" => "telnumber",
	"telnumber_sec" => "mobilenumber",
	"number_add" => "telnumber_additional",
	"email" => "email",
	"gender" => "gender",
	"parent_type" => "person_type"
    );
    // Mapping Max Length from db
    private static $MAX_LENGTH = array(
	"name" => "50",
	"surname" => "70",
	"telnumber_prim" => "15",
	"telnumber_sec" => "25",
	"number_add" => "25",
	"email" => "50",
    );

    public function init()
    {
	$this->setMethod('post');

	// Validators
	$this->emailValidator = new Zend_Validate_Db_NoRecordExists(array('table' => 'person', 'field' => 'email'));
	$valid_notempty = new Zend_Validate_NotEmpty();
	$valid_own_alpha = new Zend_Validate_Regex(array('pattern' => '~^[A-ZÄÖÜa-zäöüß]+([-, ][A-ZÄÖÜa-zäöüß]+)*$~i'));
	$valid_own_alpha->setMessage(' %value% ist nicht erlaubt. Erlaubte Zeichen: a-z, äüöß,  , - ');
	$valid_own_phone = new Zend_Validate_Regex(array('pattern' => '~^(\+)?(\([0-9]+\)\-?\s?)*([-, ,/]?[0-9])+([-, ,/][0-9]+)*$~i'));
	$valid_own_phone->setMessage(' %value% ist keine erlaubte Telefonnummer. Erlaubte Zeichen: z.B.: +49,(+49) [-, /,  ] Vorwahl [-, /,  ] Nummer');
	//$valid_own_phone = new Zend_Validate_Regex(array('pattern' => '~^(\+)?(\([0-9]+\)\-?\s?)*([0-9]+\-[0-9]+)*([0-9]+)*$~i'));


	// Personal data Form Elements
	$this->addElement('select', $this::$DB_MAPPING['gender'], array(
	    'label' => 'Anrede:',
	    'required' => true,
	    'MultiOptions' => (array(
	"Weiblich" => "Frau",
	"Männlich" => "Herr")),
	    'validators' => array()
	));

	$this->addElement('text', $this::$DB_MAPPING['name'], array(
	    'label' => 'Vorname:*',
	    'required' => true,
	    'filters' => array('StringTrim'),
	    'validators' => array($valid_notempty, $valid_own_alpha,
		array(
		    "StringLength",
		    false,
		    array(
			'max' => $this::$MAX_LENGTH['name'])),
		)));

	$this->addElement('text', $this::$DB_MAPPING['surname'], array(
	    'label' => 'Nachname:*',
	    'required' => true,
	    'filters' => array('StringTrim'),
	    'validators' => array($valid_notempty, $valid_own_alpha,
		array("StringLength", false,
		    array('max' => $this::$MAX_LENGTH['surname'])),
		)));

	$this->addElement('text', $this::$DB_MAPPING['telnumber_prim'], array(
	    'label' => 'Telefonnummer:*',
	    'required' => true,
	    'filters' => array('StringTrim'),
	    'validators' => array($valid_notempty, $valid_own_phone,
		array("StringLength",
		    false,
		    array(
			'max' => $this::$MAX_LENGTH['telnumber_prim'])),
		)));

	$this->addElement('text', $this::$DB_MAPPING['telnumber_sec'], array(
	    'label' => 'Mobil Telefonnummer:',
	    'required' => false,
	    'filters' => array('StringTrim'),
	    'validators' => array( $valid_own_phone,
		array("StringLength",
		    false,
		    array(
			'max' => $this::$MAX_LENGTH['telnumber_sec'])),
		)));

	$this->addElement('text', $this::$DB_MAPPING['email'], array(
	    'label' => 'E-Mail:',
	    'required' => false,
	    'filters' => array('StringTrim', 'Null'),
	    'validators' => array(
		array("EmailAddress"),
		array($this->emailValidator),
		array("StringLength",
		    false,
		    array(
			'max' => $this::$MAX_LENGTH['email'])),
		)));
    }

    public function excludePersonIdFromDbExistEmailValidation($id)
    {
	$this->emailValidator->setExclude(array('field' => 'id', 'value' => $id));
    }

}

